package com.rock.common.util;

public class LoadedInfo {

	// 边框，字符型边框
	private String borderChar = "#";
	// 宽度
	private int width = 50;
	// 内左边距
	private int insideLeftMargin = 10;
	// 外左边距
	private int outsideLeftMargin = 10;
	// 字符信息
	private StringBuffer printInfo = new StringBuffer();

	public String getBorderChar() {
		return borderChar;
	}

	public void setBorderChar(String borderChar) {
		this.borderChar = borderChar;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getInsideLeftMargin() {
		return insideLeftMargin;
	}

	public void setInsideLeftMargin(int insideLeftMargin) {
		this.insideLeftMargin = insideLeftMargin;
	}

	public int getOutsideLeftMargin() {
		return outsideLeftMargin;
	}

	public void setOutsideLeftMargin(int outsideLeftMargin) {
		this.outsideLeftMargin = outsideLeftMargin;
	}

	public void begin() {
		printInfo.append("\n");
		// 加入第一行
		for (int i = 0; i < outsideLeftMargin; i++) {
			// 外边距空格
			printInfo.append(" ");
		}
		for (int i = 0; i < width; i++) {
			printInfo.append(borderChar);
		}
		printInfo.append("\n");
	}

	public void addRow(String info) {
		for (int i = 0; i < outsideLeftMargin; i++) {
			// 外边距空格
			printInfo.append(" ");
		}
		if (info != null && StringUtil.isNotBlank(info)) {
			printInfo.append(borderChar);
			// 左边距
			for (int i = 0; i < insideLeftMargin; i++) {
				printInfo.append(" ");
			}
			if (info.length() >= width - insideLeftMargin - 1) {
				// 信息超长后截取
				info = info.substring(0, width - insideLeftMargin - 8) + "...";
			}
			printInfo.append(info);
			int num = width - 1 - insideLeftMargin - info.length() - 1;// 补充的空格
			for (int i = 0; i < num; i++) {
				printInfo.append(" ");
			}
			printInfo.append(borderChar);
			printInfo.append("\n");
		} else {
			// info 为null 时添加空行
			printInfo.append(borderChar);
			for (int i = 0; i < width - 2; i++) {
				printInfo.append(" ");
			}
			printInfo.append(borderChar);
			printInfo.append("\n");
		}
	}

	public void end() {
		// 最后一行
		for (int i = 0; i < outsideLeftMargin; i++) {
			// 外边距空格
			printInfo.append(" ");
		}
		for (int i = 0; i < width; i++) {
			printInfo.append(borderChar);
		}
		printInfo.append("\n");
		System.out.println(printInfo.toString());
	}

	public static void main(String[] args) {
		LoadedInfo overInfo = new LoadedInfo();
		overInfo.setBorderChar("8");
		overInfo.setWidth(80);
		overInfo.setInsideLeftMargin(20);
		overInfo.begin();
		overInfo.addRow("");
		overInfo.addRow(null);
		overInfo.addRow("helooooooooooooooheloooooooooooooohelooooooooooooooheloooooooooooooohelooooooooooooooheloooooooooooooohelooooooooooooooheloooooooooo55");
		overInfo.addRow("helo");
		overInfo.addRow("jdk version:88888888");
		overInfo.addRow("");
		overInfo.addRow(null);
		overInfo.end();
	}
}
